#region Header

/*

The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.

The Original Code is OpenFAST.

The Initial Developer of the Original Code is The LaSalle Technology
Group, LLC.  Portions created by Shariq Muhammad
are Copyright (C) Shariq Muhammad. All Rights Reserved.

Contributor(s): Shariq Muhammad <shariq.muhammad@gmail.com>

*/

#endregion Header

namespace OpenFAST.Template.Loader
{
	using ErrorHandler = OpenFAST.Error.ErrorHandler;

	public class ParsingContext
	{


		internal static readonly ParsingContext NULL = new ParsingContext();

		private readonly ParsingContext parent;

		private string dictionary;
		private ErrorHandler errorHandler;
		private System.Collections.IList fieldParsers;
		private QName name;
		private string namespace_Renamed;
		private string templateNamespace;
		private TemplateRegistry templateRegistry;
		private System.Collections.IDictionary typeMap;





		static ParsingContext()
		{
			NULL.Dictionary = "global";
			NULL.Namespace = "";
			NULL.TemplateNamespace = "";
		}

		public ParsingContext()
			: this(NULL)
		{
		}

		public ParsingContext(ParsingContext parent)
		{
			this.parent = parent;
		}

		public ParsingContext(System.Xml.XmlElement node, ParsingContext parent)
		{
			this.parent = parent;
			if (node.HasAttribute("templateNs"))
				TemplateNamespace = node.GetAttribute("templateNs");
			if (node.HasAttribute("ns"))
				Namespace = node.GetAttribute("ns");
			if (node.HasAttribute("dictionary"))
				Dictionary = node.GetAttribute("dictionary");
			if (node.HasAttribute("name"))
				setName(new QName(node.GetAttribute("name"), Namespace));
		}



		#region Properties

		public string Dictionary
		{
			get
			{
				if (dictionary == null)
					return parent.Dictionary;
				return dictionary;
			}

			set
			{
				dictionary = value;
			}
		}

		public virtual ErrorHandler ErrorHandler
		{
			get
			{
				if (errorHandler == null)
					return parent.ErrorHandler;
				return errorHandler;
			}

			set
			{
				errorHandler = value;
			}
		}

		public virtual System.Collections.IList FieldParsers
		{
			get
			{
				if (fieldParsers == null)
					return parent.FieldParsers;
				return fieldParsers;
			}

			set
			{
				fieldParsers = value;
			}
		}

		public string Namespace
		{
			get
			{
				if (namespace_Renamed == null)
					return parent.Namespace;
				return namespace_Renamed;
			}

			set
			{
				namespace_Renamed = value;
			}
		}

		public virtual ParsingContext Parent
		{
			get
			{
				return parent;
			}
		}

		public string TemplateNamespace
		{
			get
			{
				if (templateNamespace == null)
					return parent.TemplateNamespace;
				return templateNamespace;
			}

			set
			{
				templateNamespace = value;
			}
		}

		public virtual TemplateRegistry TemplateRegistry
		{
			get
			{
				if (templateRegistry == null)
					return parent.TemplateRegistry;
				return templateRegistry;
			}

			set
			{
				templateRegistry = value;
			}
		}

		public virtual System.Collections.IDictionary TypeMap
		{
			get
			{
				if (typeMap == null)
					return parent.TypeMap;
				return typeMap;
			}

			set
			{
				typeMap = value;
			}
		}

		#endregion Properties



		public virtual void AddFieldParser(FieldParser parser)
		{
			FieldParsers.Add(parser);
		}

		public virtual FieldParser GetFieldParser(System.Xml.XmlElement element)
		{
			System.Collections.IList parsers = FieldParsers;
			for (int i = parsers.Count - 1; i >= 0; i--)
			{
				var fieldParser = ((FieldParser)parsers[i]);
				if (fieldParser.CanParse(element, this))
					return fieldParser;
			}
			return null;
		}

		public virtual QName GetName()
		{
			return name;
		}

		private void setName(QName qname)
		{
			name = qname;
		}


	}
}